﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
using System.Data;
namespace DAO
{
    public class HoaDonDAO
    {
        public static int ThemHoaDon(HoaDonDTO dv)
        {
            string sql;
            sql = "Insert into HoaDon (MaHoaDon,NgayLapHoaDon, MaKhachHang ) values ('" + dv.MaHoaDon + "','" + dv.NgayLapHoaDon + "','" + dv.KhachHang.MaKhachHang + "')";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }

        public static HoaDonDTO LayThongTinHoaDon(String maHD)
        {
            HoaDonDTO hd = new HoaDonDTO();
            string sql = "Select * From HoaDon Where MaHoaDon = '" + maHD + "";
            DataTable dt = DataAccess.ExcuQuery(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                hd.MaHoaDon = Convert.ToString(dt.Rows[0]["MaHoaDon"]);
                hd.NgayLapHoaDon = Convert.ToDateTime(dt.Rows[0]["NgayLapHoaDon"]);
                hd.TrangThai = Convert.ToInt32(dt.Rows[0]["TrangThai"]);
                hd.KhachHang = KhachHangDAO.LayThongTinKhachHang(dt.Rows[0]["MaKhachHang"].ToString());
            }
            return hd;
        }
        public static int Xoa(String ma)
        {
            string sql;
            sql = "Update HoaDon  Set TrangThai=0 where MaHoaDon = '" + ma + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static ArrayList LayDanhSach()
        {
            ArrayList arr = new ArrayList();
            string sql;
            sql = "Select * From HoaDon where TrangThai=1";
            DataTable dt = DataAccess.ExcuQuery(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                HoaDonDTO dv = new HoaDonDTO();
                dv.MaHoaDon = Convert.ToString(dt.Rows[i]["MaHoaDon"]);
                dv.NgayLapHoaDon = Convert.ToDateTime(dt.Rows[i]["NgayLapHoaDon"]);
                dv.TrangThai = Convert.ToInt32(dt.Rows[i]["TrangThai"]);
                arr.Add(dv);
            }
            return arr;
        }
        public static int CapNhat(HoaDonDTO hd)
        {
            string sql;
            sql = "Update HoaDon Set MaKhachHang= '" + hd.KhachHang.MaKhachHang + "' ,NgayLapHoaDon='" + hd.NgayLapHoaDon + "', TrangThai= " + hd.TrangThai + " where MaHoaDon = '" + hd.MaHoaDon + "' ";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static ArrayList LayDanhSachTheoKhachVaThoiGian(String maKH, DateTime thoigian)
        {
            ArrayList arr = new ArrayList();
            string sql;
            sql = "Select * From HoaDon where MONTH(NgayLapHoaDon)=" + thoigian.Month + " and YEAR(NgayLapHoaDon)=" + thoigian.Year + " and MaKhachHang='" + maKH + "' AND TrangThai=1";
            DataTable dt = DataAccess.ExcuQuery(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                HoaDonDTO dv = new HoaDonDTO();
                dv.MaHoaDon = Convert.ToString(dt.Rows[i]["MaHoaDon"]);
                dv.NgayLapHoaDon = Convert.ToDateTime(dt.Rows[i]["NgayLapHoaDon"]);
                dv.TrangThai = Convert.ToInt32(dt.Rows[i]["TrangThai"]);
                arr.Add(dv);
            }
            return arr;
        }
    }
}
